package com.example.administrator.hqq.fragment;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.activity.LogInActivity;
import com.example.administrator.hqq.activity.mine.AmendUserDataActivity;
import com.example.administrator.hqq.activity.mine.MessageAlertActivity;
import com.example.administrator.hqq.activity.mine.MyFavoriteActivity;
import com.example.administrator.hqq.activity.mine.MyForumActivity;
import com.example.administrator.hqq.activity.module.AddModuleActivity;
import com.example.administrator.hqq.base.MyApplication;
import com.example.administrator.hqq.base.MyFragemnt;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.config.UrlFactory;
import com.example.administrator.hqq.utils.AlertDialogUtil;
import com.example.administrator.hqq.utils.FastClickUtils;
import com.example.administrator.hqq.utils.transform.GlideCircleTransform;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;
import okhttp3.Call;

/**
 * 首页fragment
 */
public class MineFragment extends MyFragemnt {

    Unbinder unbinder;
    @BindView(R.id.user_pic)
    ImageView userPic;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.my_favorite)
    ImageView myFavorite;
    @BindView(R.id.my_favorite_auto)
    AutoRelativeLayout myFavoriteAuto;
    @BindView(R.id.my_forum)
    ImageView myForum;
    @BindView(R.id.my_forum_auto)
    AutoRelativeLayout myForumAuto;
    @BindView(R.id.my_module)
    ImageView myModule;
    @BindView(R.id.my_module_auto)
    AutoRelativeLayout myModuleAuto;
    @BindView(R.id.message_alert)
    ImageView messageAlert;
    @BindView(R.id.message_alert_auto)
    AutoRelativeLayout messageAlertAuto;
    @BindView(R.id.log_out_auto)
    AutoLinearLayout logOutAuto;
    private View inflate;
    private final int MAX_CHAR_NUM = 20;
    private String TAG = this.getClass().getSimpleName();
    private Intent intent;
    private String name;
    private AddModule addModule;
    private String url;

    public void setAddModule(AddModule addModule) {
        this.addModule = addModule;
    }

    public MineFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflate = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        initdata();
        EventBus.getDefault().register(this);
        return inflate;
    }

    private void initdata() {
        OkHttpUtils.get().url(UrlFactory.getUserInfo())
                .addParams(LoongsConfig.user_id, SPUtils.getInstance().getString(LoongsConfig.user_id))
                .build().execute(new StringCallback() {
            private String url;

            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e(TAG, "onResponse: " + response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getInt(LoongsConfig.code) == 200) {
                        name = jsonObject.getString(LoongsConfig.nickname);
                        url = jsonObject.getString(LoongsConfig.url);
                        Glide.with(getContext()).load(url)
                                .transform(new CenterCrop(getContext()), new GlideCircleTransform(getContext()))
                                .into(userPic);
                        userName.setText(name);
                        RongIM.getInstance().refreshUserInfoCache(new UserInfo(LoongsConfig.getUser_id(),name, Uri.parse(url)));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.user_pic, R.id.my_favorite_auto, R.id.my_forum_auto, R.id.my_module_auto, R.id.message_alert_auto, R.id.log_out_auto,R.id.user_name})
    public void onViewClicked(View view) {
        if (FastClickUtils.isFastClick())
        {
            switch (view.getId()) {
                case R.id.user_pic:
                case R.id.user_name:
                    intent = new Intent(getContext(), AmendUserDataActivity.class);
                    intent.putExtra(LoongsConfig.user_name,name);
                    intent.putExtra(LoongsConfig.user_pic,url);
                    startActivity(intent);
                    break;
                case R.id.my_favorite_auto:
                    intent = new Intent(getActivity(), MyFavoriteActivity.class);
                    startActivity(intent);
                    break;
                case R.id.my_forum_auto:
                    intent = new Intent(getActivity(), MyForumActivity.class);
                    startActivity(intent);
                    break;
                case R.id.my_module_auto:
                    if (addModule!=null)
                    {
                        addModule.addModule("");
                    }
                    break;
                case R.id.message_alert_auto:
                    intent = new Intent(getActivity(), MessageAlertActivity.class);
                    startActivity(intent);
                    break;
                case R.id.log_out_auto:
                    AlertDialogUtil.showTitleAlertDialog(getContext(), "确定要退出账号吗","确定","取消", new AlertDialogUtil.AlertDialogClick.ConfirmClick() {
                        @Override
                        public void confirm() {
                            Intent intent = new Intent(getActivity(), LogInActivity.class);
                            startActivity(intent);
                            MyApplication.deleteAlias(LoongsConfig.getUser_id());
                            SPUtils.getInstance().put(LoongsConfig.user_id,"");
                            SPUtils.getInstance().put(LoongsConfig.loginToken,"");
                            getActivity().finish();
                        }
                    });
                    break;
            }
        }
    }
    public interface AddModule{
        void addModule(String s);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Even(String s){
       if (s.equals(LoongsConfig.updateMine))
       {
           initdata();
       }
    }

}
